package models;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import util.DBAction;
import util.MyLogAction;

public class ChangesData {

	private int changeID;
	private String updateInfo;
	private String modificationTime;
	private int issueID;
	
	public int getChangeID() {
		return changeID;
	}

	public void setChangeID(int changeID) {
		this.changeID = changeID;
	}

	public String getUpdateInfo() {
		return updateInfo;
	}

	public void setUpdateInfo(String updateInfo) {
		this.updateInfo = updateInfo;
	}

	public String getModificationTime() {
		return modificationTime;
	}

	public void setModificationTime(String modificationTime) {
		this.modificationTime = modificationTime;
	}

	public int getIssueID() {
		return issueID;
	}

	public void setIssueID(int issueID) {
		this.issueID = issueID;
	}

	public int update() {
		String updateSQL = 
			    "update changes "
			    + "set update_info = '" + updateInfo + "', modified_time = '" + modificationTime + 
				"', issue_id = '" + issueID + 
				"where change_id = " + changeID;
		return DBAction.update(updateSQL);
	}

	public int insert() {
		String insertSQL = "insert into changes "
				+ "(update_info, modified_time, issue_id) " + "values('" + updateInfo
				+ "', '" + modificationTime + "', " + issueID +");";
		return DBAction.update(insertSQL);
	}
	
	public static ArrayList<ChangesData> find(String selectionModifier) {
		ArrayList<ChangesData> selection = new ArrayList<ChangesData>();
		ResultSet resultSet = null;
		String query = "select * "+
				"from changes " + selectionModifier;
		Connection connection = DBAction.getConnection();
		resultSet = DBAction.readFromDB(query, connection);
		try {
			while (resultSet.next()) {
				ChangesData chngAct = new ChangesData();
				chngAct.changeID=resultSet.getInt("change_id");
				chngAct.updateInfo=resultSet.getString("update_info");
				chngAct.modificationTime=resultSet.getString("modified_time");
				chngAct.issueID=resultSet.getInt("issue_id");
				selection.add(chngAct);
			}
		} catch (SQLException e) {
            MyLogAction.myCatch("ChangesData.java", 43, e);
			e.printStackTrace();
		}
		DBAction.close(resultSet);
		DBAction.close(connection);
		return selection;
	}

	public static ChangesData findOne(String selectionModifier) {
		ResultSet resultSet = null;
		String query = "select * "+
				"from changes " + selectionModifier;
		Connection connection = DBAction.getConnection();
		resultSet = DBAction.readFromDB(query, connection);
		try {
			if (resultSet.next()) {
				ChangesData chngAct = new ChangesData();
				chngAct.changeID=resultSet.getInt("change_id");
				chngAct.updateInfo=resultSet.getString("update_info");
				chngAct.modificationTime=resultSet.getString("modified_time");
				chngAct.issueID=resultSet.getInt("issue_id");
				DBAction.close(resultSet);
				DBAction.close(connection);
				return chngAct;
			}
		} catch (SQLException e) {
            MyLogAction.myCatch("ChangesData.java", 43, e);
			e.printStackTrace();
		}
		DBAction.close(resultSet);
		DBAction.close(connection);
		return null;
	}
	
}
